package com.example.fish.service.impl;

import com.example.fish.mapper.GearMapper;
import com.example.fish.pojo.Gear;
import com.example.fish.pojo.GearExample;
import com.example.fish.service.GearService;
import com.example.fish.service.ex.GearDeleteException;
import com.example.fish.service.ex.GearInsertException;
import com.example.fish.service.ex.GearUpdateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GearServiceImpl implements GearService {

    @Autowired
    private GearMapper gearMapper;

    /***
     * 查询渔具信息
     * @param userid 用户id
     * @return 渔具信息的list集合
     */
    @Override
    public List<Gear> selectGearsByUserId(Integer userid) {
        GearExample gearExample=new GearExample();
        gearExample.createCriteria().andUseridEqualTo(userid);
        List<Gear> gears = gearMapper.selectByExample(gearExample);
        return gears;
    }

    /***
     * 保存渔具信息的方法
     * @param userid 用户id
     * @param gear 渔具对象Gear
     * @return 保存结果
     */
    @Override
    public Integer insertGear(Integer userid, Gear gear) {
        gear.setUserid(userid);
        int insert = gearMapper.insert(gear);
        if (insert == 0) {
            throw new GearInsertException("保存渔具信息失败！");
        }
        return insert;
    }

    @Override
    public Integer deleteGear(Integer gearid) {
        int i = gearMapper.deleteByPrimaryKey(gearid);
        if (i == 0){
            throw new GearDeleteException("渔具删除失败");
        }
        return i;
    }

    @Override
    public Integer updateGear(Gear gear) {
        int i = gearMapper.updateByPrimaryKey(gear);
        if (i == 0){
            throw new GearUpdateException("信息修改失败！");
        }
        return i;
    }
}
